﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SilverlightUpload
{
    public partial class PicItems : UserControl
    {
        public List<PicItemBase> Items = new List<PicItemBase>();

        public int FilesCount
        {
            get { return Items.Count; }
        }

        public long TotalSize
        {
            get { return Items.Sum(s => s.FileSize); }
        }

        private ScrollHelper helper;
        public PicItems()
        {
            InitializeComponent();
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            helper = new ScrollHelper(Scroll);
        }

        public void Add(FileInfo fileInfo)
        {
            PicItem item = new PicItem();
            PicItemBase it = new PicItemBase();
            it.File = fileInfo;
            it.Description = it.File.Name;
            item.Item = it;
            item.Margin=new Thickness(3);
            item.HorizontalAlignment = HorizontalAlignment.Stretch;
            Items.Add(it);
            stack.Children.Add(item);
            txtSumeray.Text = string.Format("预备上传的文件总数：{0}，总大小：{1}。", FilesCount, new ByteConverter().Convert(TotalSize, this.GetType(), null, null).ToString());
            item.OnItemRemove +=item_OnItemRemove;
        }

        private void item_OnItemRemove(object sender, PicItem.ItemRemoveEventArgument e)
        {
            PicItem item = sender as PicItem;
            if (item != null)
            {
                stack.Children.Remove(item);
                txtSumeray.Text = string.Format("预备上传的文件总数：{0}，总大小：{1}。", FilesCount, new ByteConverter().Convert(TotalSize, this.GetType(), null, null).ToString());
                Items.Remove(item.Item);
            }
        }
    }
}
